package com.wzxy.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzxy.dao.IBaseDao;
import com.wzxy.service.IBaseService;
import com.wzxy.utils.Page;


@Service("baseService")
@Transactional
public class BaseServiceImpl<T> implements IBaseService<T>{
	
	protected IBaseDao<T> baseDao;
	
	public void setBaseDao(IBaseDao<T> baseDao) {
		this.baseDao = baseDao;
	}

	public void add(T t) {
		baseDao.add(t);
	}

	public void delete(Serializable id) {
		baseDao.delete(id);	
	}

	public void update(T t) {
		baseDao.update(t);		
	}

	public T load(Serializable id) {
		return baseDao.load(id);
	}

	public List<T> list(String hql, Object[] args) {
		return baseDao.list(hql, args);
	}

	public void flush() {
		baseDao.flush();		
	}

	@SuppressWarnings("rawtypes")
	public Page queryForPage(int currentPage, int pageSize, String hql) {
				
		Page<T> page = new Page<T>();
		//总记录条数
		int allRow = baseDao.getAllRowCount(hql, null);
		//当前页开始记录
		int offset = page.countOffset(currentPage,pageSize); 
		//分页查询结果集
		List<T>list = baseDao.queryForPage(offset, pageSize, hql);
		
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        
		return page;
	}

	public int getAllRowCount(String hql, Object[] args) {
		return baseDao.getAllRowCount(hql, args);
	}
	
}
